//
//  AdSlideView.m
//  AdvertisementView
//
//  Created by wesleyxiao on 14-3-24.
//  Copyright (c) 2014年 wesleyxiao. All rights reserved.
//

#import "AdSlideView.h"

@interface AdSlideView ()

@end
#define  refreshInterval  5.0           //幻灯片刷新时间
#define  AdWidth  320
#define  AdHeight 200
@implementation AdSlideView

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [NSTimer scheduledTimerWithTimeInterval:refreshInterval target: self selector: @selector(handleTimer:)  userInfo:nil  repeats: YES];
    Arr=[[NSArray alloc]initWithObjects:
         @"http://t1.baidu.com/it/u=2,975971937&fm=19&gp=0.jpg",
         @"http://t2.baidu.com/it/u=2,1046468158&fm=19&gp=0.jpg",
         @"http://t3.baidu.com/it/u=2,1918941926&fm=19&gp=0.jpg",nil];
    [self AddImg:Arr];
    [self setCurrentPage:_pageCtl.currentPage];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void)AddImg:(NSArray*)arr{
    [self.scrollView setContentSize:CGSizeMake(AdWidth*[arr count], AdHeight)];
   self.pageCtl.numberOfPages=[arr count];
    
    for ( int i=0; i<[arr count]; i++) {
        NSString *url=[arr objectAtIndex:i];
        UIButton *img=[[UIButton alloc]initWithFrame:CGRectMake(AdWidth*i, 0, AdWidth, AdHeight)];
//        [self.scrollView addTarget:self action:@selector(Action) forControlEvents:UIControlEventTouchUpInside];
        [self.scrollView addSubview:img];
        NSString * name  = nil;
        if (i == 0) {
            name = @"6a63f6246b600c336e14b58c184c510fd9f9a101.jpg";
        }
        else if (i==1 )
        {
            name = @"u=2,975971937&fm=19&gp=0.jpg";
        }
        else
        {
            name = @"u=2,1046468965&fm=19&gp=0.jpg";
        }
        UIImage * i = [UIImage imageNamed:name];
       [img setImage:i forState:UIControlStateNormal];
//        UIImageFromURL( [NSURL URLWithString:url], ^( UIImage * image )
//                       {
//                           [img setImage:image forState:UIControlStateNormal];
//                       }, ^(void){
//                       });
    }
    
}

#pragma mark - scrollView && page
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    self.pageCtl.currentPage=scrollView.contentOffset.x/AdWidth;
    [self setCurrentPage:self.pageCtl.currentPage];
    
}

- (void) setCurrentPage:(NSInteger)secondPage
{
    
}

#pragma mark - 下载图片
void UIImageFromURL( NSURL * URL, void (^imageBlock)(UIImage * image), void (^errorBlock)(void) )
{
    dispatch_async( dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0 ), ^(void)
                   {
                       NSData * data = [[NSData alloc] initWithContentsOfURL:URL] ;
                       UIImage * image = [[UIImage alloc] initWithData:data];
                       dispatch_async( dispatch_get_main_queue(), ^(void){
                           if( image != nil )
                           {
                               imageBlock( image );
                           } else {
                               errorBlock();
                           }
                       });
                   });
}



#pragma mark - 5秒换图片
- (void) handleTimer: (NSTimer *) timer
{
    
        if (!Tend) {
           self.pageCtl.currentPage++;
            if (self.pageCtl.currentPage==self.pageCtl.numberOfPages-1) {
                Tend=YES;
            }
        }else{
            self.pageCtl.currentPage--;
            if (self.pageCtl.currentPage==0)
            {
                Tend=NO;
            }
        }
        
        [UIView animateWithDuration:0.7 //速度0.7秒
                         animations:^{//修改坐标
                             self.scrollView.contentOffset = CGPointMake(self.pageCtl.currentPage*320,0);
                         }];
    
}


@end
